from django import forms
from customtags.decorators import block
from pirate_core.views import namespace_get
from pirate_core.forms import FormMixin
from pirate_permissions.models import PermissionsGroup

get_namespace = namespace_get("pp_permissions")

@block
def pp_permgroup_list(context, nodelist, *args, **kwargs):

    context.push()
    namespace = get_namespace(context)

    active = kwargs.pop('active', None)
    if active is not None:
        active = True if active == "True" else False
        namespace["groups"] = PermissionsGroup.objects.filter(is_active=active).all()
    else:
        namespace["groups"] = PermissionsGroup.objects.all()

    output = nodelist.render(context)
    context.pop()

    return output


@block 
def pp_permgroup_form(context, nodelist, *args, **kwargs):

    context.push()
    namespace = get_namespace(context)

    POST = kwargs.pop('POST', None)
    path = kwargs.pop('path', "/")

    form = GroupForm.create(POST, path)

    namespace['form'] = form
    output = nodelist.render(context)
    context.pop()

    return output


class GroupForm(forms.ModelForm, FormMixin):
    class Meta:
        model = PermissionsGroup 

